package com.example.restatr.Atcivity.KitchenHelper;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.KitchenHelper.Bean.DishesBean;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyCollectionAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private RecyclerView rv;
    private LinearLayout Null;
    private TextView nullText;
    private List<DishesBean.RowsBean> rowsBeans = new ArrayList<>();
    private adapter adapter = new adapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_my_favorite_at );

        toolbar = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        Null = findViewById( R.id.Null );
        nullText = findViewById( R.id.null_text );
        rv = findViewById( R.id.rv );
        Tools.SetBarTitle( toolbar, barTitle, "我的收藏", search, this, false );
        nullText.setText( "暂无收藏..." );
    }

    @Override
    protected void onResume() {
        super.onResume();
        rowsBeans.clear();
        GetMyCollection();
    }

    private void GetMyCollection() {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/kitchen-helper/dishes-collection/list", MainActivity.sp.getString( "token", "" ) );
            DishesBean dishesBean = new Gson().fromJson( token, DishesBean.class );
            rowsBeans.addAll( dishesBean.getRows() );
            runOnUiThread( () -> {
                if (rowsBeans.size() > 0) {
                    rv.setAdapter( adapter );
                } else {
                    Null.setVisibility( View.VISIBLE );
                }
            } );
        } ).start();
    }

    class adapter extends RecyclerView.Adapter<adapter.viewHolder> {
        @NonNull
        @Override
        public adapter.viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate( R.layout.list_item_layout, parent, false );
            return new adapter.viewHolder( inflate );
        }

        @Override
        public void onBindViewHolder(@NonNull adapter.viewHolder holder, int position) {
            Glide.with( MyCollectionAt.this )
                    .load( Tools.ip + rowsBeans.get( position ).getImgUrl() )
                    .transform( new RoundedCorners( 15 ) )
                    .into( holder.tp );
            holder.title1.setText( rowsBeans.get( position ).getName() );
            holder.title2.setText( rowsBeans.get( position ).getIngredients() );
            holder.text3.setText( String.valueOf( rowsBeans.get( position ).getLikeCount() ) );
            holder.text4.setText( String.valueOf( rowsBeans.get( position ).getCollectionCount() ) );
            if (rowsBeans.get( position ).isMyLikeState()) {
                holder.icon3.setImageResource( R.drawable.ic_baseline_favorite_24 );
            } else {
                holder.icon3.setImageResource( R.drawable.ic_baseline_favorite_border_24 );
            }
            if (rowsBeans.get( position ).isMyCollectionState()) {
                holder.icon4.setImageResource( R.drawable.ic_round_star_24 );
            } else {
                holder.icon4.setImageResource( R.drawable.ic_round_star_border_24 );
            }
            //点赞/取消点赞某个菜品
            holder.ll3.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread( () -> {
                        try {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put( "dishesId", rowsBeans.get( position ).getId() );
                            jsonObject.put( "newState", !rowsBeans.get( position ).isMyLikeState() );
                            String token = Tools.Post( "/prod-api/api/kitchen-helper/dishes-like",
                                    MainActivity.sp.getString( "token", "" ), jsonObject.toString() );
                            JSONObject jx = new JSONObject( token );
                            String msg = jx.getString( "msg" );
                            int code = jx.getInt( "code" );
                            runOnUiThread( () -> {
                                Toast.makeText( MyCollectionAt.this, msg, Toast.LENGTH_SHORT ).show();
                                if (code == 200) {
                                    rowsBeans.get( position ).setMyLikeState( !rowsBeans.get( position ).isMyLikeState() );
                                    if (rowsBeans.get( position ).isMyLikeState()) {
                                        rowsBeans.get( position ).setLikeCount( rowsBeans.get( position ).getLikeCount() + 1 );
                                    } else {
                                        rowsBeans.get( position ).setLikeCount( rowsBeans.get( position ).getLikeCount() - 1 );
                                    }
                                    adapter.notifyItemChanged( position );
                                }
                            } );
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } ).start();

                }
            } );
            //收藏/取消收藏某个菜品
            holder.ll4.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread( () -> {
                        try {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put( "dishesId", rowsBeans.get( position ).getId() );
                            jsonObject.put( "newState", !rowsBeans.get( position ).isMyCollectionState() );
                            String token = Tools.Post( "/prod-api/api/kitchen-helper/dishes-collection",
                                    MainActivity.sp.getString( "token", "" ), jsonObject.toString() );
                            JSONObject jx = new JSONObject( token );
                            String msg = jx.getString( "msg" );
                            int code = jx.getInt( "code" );
                            runOnUiThread( () -> {
                                Toast.makeText( MyCollectionAt.this, msg, Toast.LENGTH_SHORT ).show();
                                if (code == 200) {
                                    rowsBeans.remove( position );
                                    if (rowsBeans.size() == 0) {
                                        Null.setVisibility( View.VISIBLE );
                                    }
                                    adapter.notifyDataSetChanged();
                                }
                            } );
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } ).start();
                }
            } );
            holder.itemView.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent( MyCollectionAt.this, DetailsOfDishesAt.class );
                    intent.putExtra( "id", rowsBeans.get( position ).getId() );
                    startActivity( intent );
                }
            } );
        }

        @Override
        public int getItemCount() {
            return rowsBeans.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            private ImageView tp;
            private TextView title1;
            private TextView title2;
            private LinearLayout ll3;
            private ImageView icon3;
            private TextView text3;
            private LinearLayout ll4;
            private ImageView icon4;
            private TextView text4;

            public viewHolder(@NonNull View itemView) {
                super( itemView );
                tp = itemView.findViewById( R.id.tp );
                title1 = itemView.findViewById( R.id.title1 );
                title2 = itemView.findViewById( R.id.title2 );
                ll3 = itemView.findViewById( R.id.ll3 );
                icon3 = itemView.findViewById( R.id.icon3 );
                text3 = itemView.findViewById( R.id.text3 );
                ll4 = itemView.findViewById( R.id.ll4 );
                icon4 = itemView.findViewById( R.id.icon4 );
                text4 = itemView.findViewById( R.id.text4 );
            }
        }
    }

}